using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;
using System.Xml;
using VoteMeDefine;



namespace VoteMe
{
    public partial class FrmLogon : Form
    {
        public FrmLogon()
        {
            InitializeComponent();
            
        }

        private void MiExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FrmLogon_Load(object sender, EventArgs e)
        {

            InitLogon();
        }

        private void InitLogon()
        {
            
            //Read users from xml, insert to CmbUsers;
            VariableDefine.meConn.Open();
            VariableDefine.meCmd.CommandText = "SELECT UserName FROM userInfo WHERE Status=1 ORDER BY ID";
            SqlCeResultSet meRs = VariableDefine.meCmd.ExecuteResultSet(VariableDefine.meRSOptions);

            CmbUser.Items.Clear();
            while (meRs.Read())
            {
                string s = meRs.GetString(0);
                CmbUser.Items.Add(s);
            }

            meRs.Close();
            VariableDefine.meConn.Close();
            


            //Set all form control;
            try
            {
                CmbUser.SelectedIndex = CmbUser.Items.Count-1;
            }
            catch 
            {

            }

            CmbUser.Enabled = true;
            BtnLogon.Enabled = true;
            BtnRegister.Enabled = true;

            BtnAdd.Visible = false;
            BtnCancel.Visible = false;
            TxtNewer.Visible = false;
            
            if (CmbUser.Items.Count < 1)
                BtnLogon.Enabled = false;


        }

        private void BtnRegister_Click(object sender, EventArgs e)
        {
            //lock logon control;
            CmbUser.Enabled = false;
            BtnLogon.Enabled = false;
            BtnRegister.Enabled = false;

            //set register visible;
            TxtNewer.Visible = true;
            BtnAdd.Visible = true;
            BtnCancel.Visible = true;

            TxtNewer.Text = @"";
            TxtNewer.Focus();
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            //check user
            //no empty
            if (TxtNewer.Text.Trim().Length == 0)
            {
                MessageBox.Show("User name cannot be empty.","Error");
                TxtNewer.Focus();
                TxtNewer.SelectAll();
                return;
                
            }
            //no same
            foreach (System.Object cmbItem in CmbUser.Items)
            {
                if (TxtNewer.Text.Trim().ToLower()  == cmbItem.ToString().Trim().ToLower())
                {
                    MessageBox.Show("User name had be defined!", "Error");
                    TxtNewer.Focus();
                    TxtNewer.SelectAll();
                    return;
                }
            }


            
            
            //add new user to db
            VariableDefine.meConn.Open();
            VariableDefine.meCmd.CommandText = "INSERT INTO userInfo (UserName,RegTime,Status) VALUES ('" + TxtNewer.Text.Trim() + "','" + DateTime.Now.ToString(VariableDefine.dateFormate) + "',1)";
            VariableDefine.meCmd.ExecuteNonQuery();
            VariableDefine.meConn.Close();
            

            //reload logon form
            InitLogon();
            return;
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            InitLogon();
            return;
        }

        private void BtnLogon_Click(object sender, EventArgs e)
        {
            VariableDefine.user = CmbUser.SelectedItem.ToString();
            FrmMain frmMain = new FrmMain();
            frmMain.Show();
            this.Visible = false;
        }
    }
}